package com.shop.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.entity.GoodsCategory;
import com.shop.service.GoodsCategoryService;

/**
 * <p>
 * 商品种类表 前端控制器
 * </p>
 *
 * @author guocb
 * @since 2019-12-17
 */
@RestController
@RequestMapping("/goodsCategory")
public class GoodsCategoryController {

	@Autowired
	GoodsCategoryService goodsCategoryService;

	@GetMapping("/cates")
	public List<GoodsCategory> cates(@RequestParam(defaultValue = "0", required = false) Integer parentId) {
		QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<GoodsCategory>();
		queryWrapper.eq(GoodsCategory.PARENT_ID, parentId);
		return goodsCategoryService.list(queryWrapper);
	}

	@GetMapping("/subCates")
	public List<GoodsCategory> subCates(@RequestParam(defaultValue = "1", required = false) Integer parentId) {
		QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<GoodsCategory>();
		queryWrapper.eq(GoodsCategory.PARENT_ID, parentId);
		// 二级
		List<GoodsCategory> cateList = goodsCategoryService.list(queryWrapper);

		// 遍历获取对应的
		cateList.forEach((cate) -> {
			// 三级菜单
			QueryWrapper<GoodsCategory> queryWrapper1 = new QueryWrapper<GoodsCategory>();
			queryWrapper1.eq(GoodsCategory.PARENT_ID, cate.getCategoryId());
			cate.setCateList(goodsCategoryService.list(queryWrapper1));
		});

		return cateList;
	}
}
